<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 分割求和
      function splitSum(n) {
        let str = n + "";
        let arr = str.split("");
        console.log(arr);
        let sum = 0;
        for (let num of arr) {
          sum += num * num;
        }
        console.log("sum", sum);
        return sum;
      }
      // splitSum(20);
      var isHappy = function (n) {
        let set = new Set();
        let sum = splitSum(n);
        while (true) {
          if (sum == 1) {
            return true;
          } else {
            if (set.has(sum)) {
              return false;
            } else {
              set.add(sum);
              sum = splitSum(sum);
            }
          }
        }
      };

      console.log(isHappy(19));
    </script>
  </body>
</html>
